package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.CommodityCategoryBusiness;
import com.ticket.sass.admin.dto.input.commodity.category.CommodityCategoryAddInput;
import com.ticket.sass.admin.dto.input.commodity.category.CommodityCategoryListInput;
import com.ticket.sass.admin.dto.input.commodity.category.CommodityCategoryUpdInput;
import com.ticket.sass.admin.dto.output.commodity.category.CommodityCategoryListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.CommodityCategoryServiceImpl;
import com.ticket.sass.admin.util.EscapeUtil;
import com.ticket.sass.admin.util.FileUtil;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.entity.tenant.CommodityCategoryEntity;
import com.ticket.sass.common.exception.enums.ErrorCode;
import com.ticket.sass.common.exception.exception.BadRequestException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ywb
 * @date 2024-03-23 15:25
 */
@Slf4j
@Service
@AllArgsConstructor
public class CommodityCategoryBusinessImpl implements CommodityCategoryBusiness {
    
    final ModelUtil modelUtil;
    final FileUtil fileUtil;
    final CommodityCategoryServiceImpl service;
    
    @Override
    public PageModel<CommodityCategoryListOutput> list(CommodityCategoryListInput input) {
        IPage<CommodityCategoryEntity> page = service.iPage(input);
        List<CommodityCategoryListOutput> outputs = modelUtil.listToList(
            page.getRecords(),
            new TypeToken<List<CommodityCategoryListOutput>>(){}.getType()
        );
        return new PageModel<>(page.getTotal(), outputs);
    }
    
    @Override
    public void add(CommodityCategoryAddInput input) {
        if (service.count(new QueryWrapper<CommodityCategoryEntity>().eq("name", input.getName())) > 0){
            throw new BadRequestException(
                ErrorCode.RECORD_NOT_EXIST,
                EscapeUtil.errorMessage("name", "分类已存在")
            );
        }
        Integer validSort = service.getValidSort(input.getSort(), null);
        input.setSort(validSort);
        
        
        fileUtil.tempFieldPro(input, "icon");
        service.save(modelUtil.mapToMap(input, CommodityCategoryEntity.class));
    }
    
    @Override
    public void upd(CommodityCategoryUpdInput input) {
        validateId(input.getId());
        // 检查是否有其他记录的名称与输入名称相同
        QueryWrapper<CommodityCategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", input.getName()).ne("id", input.getId());
        if (service.count(queryWrapper) > 0) {
            throw new BadRequestException(
                ErrorCode.RECORD_EXIST,
                EscapeUtil.errorMessage("name", "分类已存在")
            );
        }
        Integer validSort = service.getValidSort(input.getSort(), input.getId());
        input.setSort(validSort);
        
        fileUtil.tempFieldPro(input, "icon");
        service.updateById(modelUtil.mapToMap(input, CommodityCategoryEntity.class));
    }
    
    private void validateId(Long id) {
        if (service.isEmpty(id)) {
            throw new BadRequestException(
                ErrorCode.RECORD_NOT_EXIST,
                EscapeUtil.errorMessage("id", ErrorCode.RECORD_NOT_EXIST.getReasonPhrase())
            );
        }
    }
}
